package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.CarCharge;
import com.group.Service.CarChargeService;
import com.group.Service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("car")
public class CarController {

    @Autowired
    private CarService carService;
    @Autowired
    private CarChargeService carChargeService;

    @ResponseBody
    @RequestMapping("queryAllParkingCharge.do")
    public JSONObject queryAllParkingCharge(Integer page,Integer limit,String numbers,Integer status){


        PageHelper.startPage(page,limit);
        List<CarCharge> list = carChargeService.getCarCharges(numbers,status);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();
        List<HashMap<String,Object>> list1 = new ArrayList();
        Iterator iterator = list_new.iterator();
        while(iterator.hasNext())
        {
            CarCharge carCharge = (CarCharge) iterator.next();
            HashMap<String,Object> hm = new HashMap();
            hm.put("numbers",carCharge.getParking().getNumbers() );
            hm.put("username",carCharge.getOwner().getUsername() );
            hm.put("payDate",carCharge.getPayDate() );
            hm.put("endDate",carCharge.getEndDate() );
            hm.put("money",carCharge.getMoney() );
            hm.put("status",carCharge.getStatus() );
            hm.put("id",carCharge.getId() );
            list1.add(hm);

        }

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list1);
        return json;
    }

    @ResponseBody
    @RequestMapping("searchParkAll.do")
    public JSONObject searchParkAll(Integer page,Integer limit,String numbers,Integer status){


        PageHelper.startPage(page,limit);
        List<CarCharge> list = carChargeService.getCarCharges(numbers,status);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();
        List<HashMap<String,Object>> list1 = new ArrayList();
        Iterator iterator = list_new.iterator();
        while(iterator.hasNext())
        {
            CarCharge carCharge = (CarCharge) iterator.next();
            HashMap<String,Object> hm = new HashMap();
            hm.put("numbers",carCharge.getParking().getNumbers() );
            hm.put("username",carCharge.getOwner().getUsername() );
            hm.put("payDate",carCharge.getPayDate() );
            hm.put("endDate",carCharge.getEndDate() );
            hm.put("money",carCharge.getMoney() );
            hm.put("status",carCharge.getStatus() );
            hm.put("id",carCharge.getId() );
            list1.add(hm);

        }

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list1);
        return json;
    }



    @ResponseBody
    @PostMapping("deleteByIds.do")
    public JSONObject deleteByIds(String ids){
        String[] list = ids.split(",");

        for (int step = 0;step<list.length;step++)
        {
            int id = Integer.parseInt(list[step]);
            carChargeService.delCarChargeByID(id);
        }

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }

    @ResponseBody
    @PostMapping("update.do")
    public JSONObject update(Integer id){
        if (id != null) {
            carChargeService.updateStatus(id);
        }
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }

}
